var region_list=[];

function get_u_tip_msg(obj,tip){
	var tmp_str=obj.attr(tip);
	return tmp_str;
}

$(function(){
	var selectedMessage="",validMessage="",failureMessage="";
	//验证会员账号
	if($("#ld_userID")[0]){
		selectedMessage=get_u_tip_msg($("#ld_userID"),"selectedMsg")||lodo_lang_js.reg.tip_1;
		validMessage=get_u_tip_msg($("#ld_userID"),"CorrectMsg");
		failureMessage=get_u_tip_msg($("#ld_userID"),"errMsg");
		var ld_userID=new LiveValidation("ld_userID",{onValid:checkuser,selectedMessage:selectedMessage,validMessage:validMessage});
		ld_userID.add(Validate.Presence);
		ld_userID.add(($("#reg_mailtouerno").val()=="1")?Validate.Email:Validate.Length,($("#reg_mailtouerno").val()=="1")?{failureMessage:failureMessage}:{minimum:4,maximum:255,failureMessage:failureMessage});
	}
	//验证会员密码
	if($("#ld_pass1")[0]){
		selectedMessage=get_u_tip_msg($("#ld_pass1"),"selectedMsg")||lodo_lang_js.reg.tip_2;
		validMessage=get_u_tip_msg($("#ld_pass1"),"CorrectMsg")||lodo_lang_js.reg.tip_3;
		$("#ld_pass1").pstrength();
		var ld_pass1=new LiveValidation("ld_pass1",{selectedMessage:selectedMessage,validMessage:validMessage,insertAfterWhatNode:$("span.pwStrength:first")[0]});
		ld_pass1.add(Validate.Presence);
		ld_pass1.add(Validate.Length,{minimum:6,maximum:255});
	}
	if($("#ld_pass2")[0]){
		selectedMessage=get_u_tip_msg($("#ld_pass1"),"selectedMsg")||lodo_lang_js.reg.tip_4;
		validMessage=get_u_tip_msg($("#ld_pass1"),"CorrectMsg")||lodo_lang_js.reg.tip_5;
		var ld_pass2=new LiveValidation("ld_pass2",{selectedMessage:selectedMessage,validMessage:validMessage});
		ld_pass2.add(Validate.Presence);
		ld_pass2.add(Validate.Confirmation,{
				failureMessage:lodo_lang_js.reg.tip_6,
				match:"ld_pass1"
		});
	}
	$("#regform input:text[r_typ='1'],#regform input:text[r_typ='2'],#regform input:text[r_typ='3'],#regform input:text[r_typ='4'],#regform input:text[r_typ='8'],#regform input:text[r_typ='9'],#regform input:text[r_typ='10'],#regform input:text[r_typ='11'],#regform input:text[r_typ='12']").each(function(){
		selectedMessage=get_u_tip_msg($(this),"selectedMsg");
		validMessage=get_u_tip_msg($(this),"CorrectMsg");
		failureMessage=get_u_tip_msg($(this),"errMsg");
		var r_typ=$(this).attr("r_typ");
		var r_rno=$(this).attr("r_rno");
		var r_id=$(this).attr("id");
		var option={selectedMessage:selectedMessage,validMessage:validMessage};
		if(r_id=="ld_code"){
			option.insertAfterWhatNode=$("#ld_code_img")[0];
			option.onValid=check_user_code;
		};
		if(r_id=="ld_mail"){
			option.onValid=checkuemail;
		};
		var tmp_obj=new LiveValidation(r_id,option);
		if(r_rno=="1"){tmp_obj.add(Validate.Presence,{failureMessage:failureMessage});}
		
		switch(r_typ){
			case "1":{
				maximum=255;
				switch(r_id){
					case "ld_mail":{
						tmp_obj.add(Validate.Email,{failureMessage:failureMessage,allowNull:true});
						break;
					}
					case "ld_addr":{
						maximum=500;
						break;
					}
				}
				tmp_obj.add(Validate.Length,{maximum:maximum});
				break;
			}
			case "2":{
				tmp_obj.add(Validate.Numericality);
				break;
			}
			case "3":{
				tmp_obj.add(Validate.Format,{
					failureMessage:lodo_lang_js.reg.tip_7,
					pattern:/^[A-Za-z]+$/,
					allowNull:true
				});
				break;
			}
			case "4":{
				tmp_obj.add(Validate.Format,{
					failureMessage:lodo_lang_js.reg.tip_8,
					pattern:/[A-Za-z0-9]+$/,
					allowNull:true
				});
				break;
			}
			case "8":{
				
				break;
			}
			case "9","10","11","12":{
				tmp_obj.add(Validate.Length,{maximum:255});
				break;
			}
			
		}
	});


	$("#regform select[r_typ='5']").each(function(){
		selectedMessage=get_u_tip_msg($(this),"selectedMsg");
		validMessage=get_u_tip_msg($(this),"CorrectMsg");
		failureMessage=get_u_tip_msg($(this),"errMsg")||lodo_lang_js.reg.tip_9;
		var r_rno=$(this).attr("r_rno");
		var withinlist=[];
		$(this).find("option[value!='']").each(function(){
			withinlist.push($(this).val());
		});
		var tmp_obj=new LiveValidation($(this).attr("id"),{selectedMessage:selectedMessage,validMessage:validMessage});
		if(r_rno=="1"){tmp_obj.add(Validate.Presence);}
		tmp_obj.add(Validate.Inclusion, { within:withinlist, failureMessage: failureMessage} );
	});

	$("#regform input:radio[r_typ='6']").each(function(){		
		var r_rno=$(this).attr("r_rno");
		var r_name=$(this).attr("name");
		var err_msg=$("#"+r_name+"_errmsg");
		if(r_rno=="1"){
			selectedMessage=get_u_tip_msg(err_msg,"selectedMsg");
			validMessage=get_u_tip_msg(err_msg,"CorrectMsg");
			failureMessage=get_u_tip_msg(err_msg,"errMsg")||lodo_lang_js.reg.tip_10;
		
		   var tmp_obj=new LiveValidation($(this).attr("id"),{insertAfterWhatNode:err_msg[0],selectedMessage:selectedMessage,validMessage:validMessage});
			tmp_obj.add(Validate.Acceptance,{failureMessage:failureMessage});
		}
	})
	
	$("#regform input:checkbox[r_typ='7']").each(function(){
		var r_rno=$(this).attr("r_rno");
		var r_name=$(this).attr("name");
		var err_msg=$("#"+r_name+"_errmsg");
		if(r_rno=="1"){
			selectedMessage=get_u_tip_msg(err_msg,"selectedMsg");
			validMessage=get_u_tip_msg(err_msg,"CorrectMsg");
			failureMessage=get_u_tip_msg(err_msg,"errMsg")||lodo_lang_js.reg.tip_11;
		   var tmp_obj=new LiveValidation($(this).attr("id"),{insertAfterWhatNode:err_msg[0],selectedMessage:selectedMessage,validMessage:validMessage});
			tmp_obj.add(Validate.Acceptance,{failureMessage:failureMessage});
		}
	})
	
	$("#regform input:text[r_typ='8']").datepicker({showAnim:"fadeIn",changeMonth: true,changeYear: true,onClose:function(dateText, inst){
		$(this).blur();
	}});
	
	if($("#reg_Region_list")[0]){Region_load(0,1,"1_",region_list);}

	
	$("#regform").submit(function(){
		if(lv_submitno){
			var options={
				//url:"regcode.asp?bn="+Math.random(),
				dataType:"json",
				success:function(data){
					removeloadding("regbotton");
					if(data.errno){alert(data.msg);return false;}
					if(data.gotono){location.href=data.gotourl};
				}
			};
			loadding("regbotton",lodo_lang_js.reg.tip_12,"",0);
			//$("#regform").submit();
			$("#regform").ajaxSubmit(options);
		}
		return false;
	});
	
})

function Region_change(e){
	var rType=$(e).attr("rType")*1;
	var rtypelist=$(e).attr("rtypelist");
	var N_rType=rType+1;
	rtypelist+=N_rType+"_";
	var seloption=$(e).find("option:selected");
	var uid=seloption.attr("value");
	var sum=seloption.attr("sum");
	if(sum>0){
		Region_load(uid,N_rType,rtypelist);
	}else{
		$("select[name^='region_list_"+rtypelist+"']").remove();	
	}
		var tmp_region_name_list="";
		$("#reg_Region_list>select[name^='region_list_']>option:selected[value!=0]").each(function(){
			//if(tmp_region_name_list!=""){tmp_region_name_list+="[@]"}
			tmp_region_name_list+=$(this).html();
		});
		$("#region_sum").val(rType);
		$("#region_name_list").val(tmp_region_name_list);
}

function Region_load(uid,rType,rtypelist,region_list){
	var selop=0;
	if(region_list){
		if(rType<=region_list.length){selop=region_list[(rType-1)];}
	}
	var selectn=$("#region_list_"+rtypelist);
	$("select[name^='region_list_"+rtypelist+"']").remove();
	if(!selectn[0]){selectn=$("<select name='region_list_"+rtypelist+"' id='region_list_"+rtypelist+"' rtype='"+rType+"' rtypelist='"+rtypelist+"' onchange='Region_change(this);'></select>");}
	selectn.html("<option value='' sum=0>"+lodo_lang_js.reg.tip_13+"</option>");
	
	var loadding=$("#reg_Region_list>span.loadding");
	if(!loadding[0]){loadding=$("<span class='loadding'></span>").appendTo($("#reg_Region_list"))}
	loadding.html(lodo_lang_js.reg.tip_14).show();
	$.getJSON(lodo_web_path+"ajax_data.asp?action=regionlist&uid="+uid+"",function(data){
		loadding.html("").hide();
		$(data.items).each(function(){
			var selected="";
			if(selop==this.id){selected="selected";if(this.n_num<=0){selop=0;}}
			selectn.append("<option value="+this.id+" sum="+this.n_num+" "+selected+">"+this.name+"</option>");
		});
		$("#reg_Region_list").append(selectn);
		if(uid==0){
			var r_rno=$("#reg_Region_list").attr("r_rno");
			var selectedMessage=get_u_tip_msg($("#reg_Region_list"),"selectedMsg");
			var validMessage=get_u_tip_msg($("#reg_Region_list"),"CorrectMsg");
			var failureMessage=get_u_tip_msg($("#reg_Region_list"),"errMsg")||lodo_lang_js.reg.tip_15;
			var withinlist=[];
			$(data.items).each(function(){
				withinlist.push(this.id);
			});
			var region_list_1_=new LiveValidation("region_list_1_",{selectedMessage:selectedMessage,validMessage:validMessage,insertAfterWhatNode:$("#reg_Region_list")[0]});
			var allowNull=true;
			if(r_rno=="1"){region_list_1_.add(Validate.Presence);allowNull=false;}
			region_list_1_.add(Validate.Inclusion, { allowNull:allowNull,within:withinlist, failureMessage: failureMessage} );
		}
		var n_rType=(rType+1);
		if(selop!=0){Region_load(selop,n_rType,rtypelist+n_rType+"_",region_list);}
	})
	
}

var chuser_ajax;
function checkuser(){
	clearTimeout(chuser_ajax);
	chuser_ajax=setTimeout(function(){
		var ld_userID=$("#ld_userID").val();
		var tip_span=$("#ld_userID").next("span");
		if(!tip_span[0]){$("#ld_userID").after("<span class='LV_validation_message'></span>");tip_span=$("#ld_userID").next("span");}
		if(ld_userID==""){tip_span.removeClass("LV_valid").addClass("LV_invalid").html("<font>"+lodo_lang_js.reg.tip_16+"</font>");return false;}
		tip_span.removeClass("LV_valid").removeClass("LV_invalid").removeClass("LV_selected").addClass("LV_loadding").html("<img src='"+lodo_web_path+"js/ajax-loader.gif'>");
		$.getJSON(lodo_web_path+"ajax_data.asp?action=chuser&uname="+ld_userID+"",function(data){
			if(data.errno){tip_span.removeClass("LV_valid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_invalid").html("<font>"+data.msg+"</font>");return false;}
			if(data.ch){tip_span.removeClass("LV_invalid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_valid").html("<font>"+lodo_lang_js.reg.tip_17+"</font>");}
			return false;
		});
	},300)
}
var chemail_ajax;
function checkuemail(){
	clearTimeout(chemail_ajax);
	chemail_ajax=setTimeout(function(){
		var ld_mail=$("#ld_mail").val();
		var r_editno=$("#ld_mail").attr("r_editno");
		var r_rno=$("#ld_mail").attr("r_rno");
		var tip_span=$("#ld_mail").next("span");
		if(!tip_span[0]){$("#ld_mail").after("<span class='LV_validation_message'></span>");tip_span=$("#ld_mail").next("span");}
		if(ld_mail==""){
			if(r_rno=="1"){
				tip_span.removeClass("LV_valid").removeClass("LV_selected").addClass("LV_invalid").html("<font>"+lodo_lang_js.reg.tip_18+"</font>");
			}else{
				tip_span.remove();
			}
			return false;
		}
		if(r_editno=="1"){r_editno=1}else{r_editno=0};
		tip_span.removeClass("LV_valid").removeClass("LV_invalid").removeClass("LV_selected").addClass("LV_loadding").html("<img src='"+lodo_web_path+"js/ajax-loader.gif'>");
		$.getJSON(lodo_web_path+"ajax_data.asp?action=chuemail&editno="+r_editno+"&email="+ld_mail+"",function(data){
			if(data.errno){tip_span.removeClass("LV_valid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_invalid").html("<font>"+data.msg+"</font>");return false;}
			if(data.ch){tip_span.removeClass("LV_invalid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_valid").html("<font>"+lodo_lang_js.reg.tip_19+"</font>");}
			return false;
		});
	},300);
}
var chusercode_ajax;
function check_user_code(){
	clearTimeout(chusercode_ajax);
	chusercode_ajax=setTimeout(function(){
		var ld_code=$("#ld_code").val();
		var tip_span=$("#ld_code_img").next("span");
		if(!tip_span[0]){$("#ld_code_img").after("<span class='LV_validation_message'></span>");tip_span=$("#ld_code_img").next("span");}
		if(ld_code==""){tip_span.removeClass("LV_valid").addClass("LV_invalid").html("<font>"+lodo_lang_js.reg.tip_20+"</font>");return false;}
		tip_span.removeClass("LV_valid").removeClass("LV_invalid").removeClass("LV_selected").addClass("LV_loadding").html("<img src='"+lodo_web_path+"js/ajax-loader.gif'>");
		$.getJSON(lodo_web_path+"ajax_data.asp?action=chusercode&code="+ld_code+"",function(data){
			if(data.errno){tip_span.removeClass("LV_valid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_invalid").html("<font>"+data.msg+"</font>");return false;}
			if(data.ch){tip_span.removeClass("LV_invalid").removeClass("LV_selected").removeClass("LV_loadding").addClass("LV_valid").html("<font>"+lodo_lang_js.reg.tip_21+"</font>");}
			return false;
		});
	},300)
}